{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "first_steps_with_tensor_flow.ipynb",
      "version": "0.3.2",
      "views": {},
      "default_view": {},
      "provenance": [],
      "collapsed_sections": [
        "ajVM7rkoYXeL",
        "ci1ISxxrZ7v0",
        "copyright-notice"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "copyright-notice",
        "colab_type": "text"
      },
      "source": [
        "#### Copyright 2017 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "copyright-notice2",
        "colab_type": "code",
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "outputs": [],
      "execution_count": 0
    },
    {
      "metadata": {
        "id": "4f3CKqFUqL2-",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " # \ud150\uc11c\ud50c\ub85c\uc6b0 \uccab\uac78\uc74c"
      ]
    },
    {
      "metadata": {
        "id": "Bd2Zkk1LE2Zr",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\ud559\uc2b5 \ubaa9\ud45c:**\n",
        "  * \ud150\uc11c\ud50c\ub85c\uc6b0\uc758 \uae30\ucd08 \uac1c\ub150\uc744 \ud559\uc2b5\ud55c\ub2e4\n",
        "  * \ud150\uc11c\ud50c\ub85c\uc6b0\uc758 `LinearRegressor` \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc785\ub825 \ud2b9\uc131 \ud558\ub098\ub97c \uae30\ubc18\uc73c\ub85c \uc9c0\uc5ed\ubcc4 \uc8fc\ud0dd \uac00\uaca9 \uc911\uc559\uac12\uc744 \uc608\uce21\ud55c\ub2e4\n",
        "  * \ud3c9\uade0 \uc81c\uacf1\uadfc \uc624\ucc28(RMSE)\ub97c \uc0ac\uc6a9\ud558\uc5ec \ubaa8\ub378 \uc608\uce21\uc758 \uc815\ud655\uc131\uc744 \ud3c9\uac00\ud55c\ub2e4\n",
        "  * \ucd08\ub9e4\uac1c\ubcc0\uc218\ub97c \uc870\uc815\ud558\uc5ec \ubaa8\ub378\uc758 \uc815\ud655\uc131\uc744 \uac1c\uc120\ud55c\ub2e4"
      ]
    },
    {
      "metadata": {
        "id": "MxiIKhP4E2Zr",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub370\uc774\ud130\uc758 \ucd9c\ucc98\ub294 1990\ub144 \uce98\ub9ac\ud3ec\ub2c8\uc544 \uc778\uad6c\uc870\uc0ac \uc790\ub8cc\uc785\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "6TjLjL9IU80G",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc124\uc815\n",
        "\uccab \ubc88\uc9f8 \uc140\uc5d0\uc11c \ud544\uc694\ud55c \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \ub85c\ub4dc\ud558\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "rVFf5asKE2Zt",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "from __future__ import print_function\n",
        "\n",
        "import math\n",
        "\n",
        "from IPython import display\n",
        "from matplotlib import cm\n",
        "from matplotlib import gridspec\n",
        "from matplotlib import pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn import metrics\n",
        "import tensorflow as tf\n",
        "from tensorflow.python.data import Dataset\n",
        "\n",
        "tf.logging.set_verbosity(tf.logging.ERROR)\n",
        "pd.options.display.max_rows = 10\n",
        "pd.options.display.float_format = '{:.1f}'.format"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ipRyUHjhU80Q",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub2e4\uc74c\uc73c\ub85c\ub294 \ub370\uc774\ud130 \uc138\ud2b8\ub97c \ub85c\ub4dc\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "9ivCDWnwE2Zx",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "california_housing_dataframe = pd.read_csv(\"https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv\", sep=\",\")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "vVk_qlG6U80j",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ud655\ub960\uc801 \uacbd\uc0ac\ud558\uac15\ubc95\uc758 \uc131\ub2a5\uc5d0 \uc545\uc601\ud5a5\uc744 \uc904 \uc218 \uc788\ub294 \uc758\ub3c4\uce58 \uc54a\uc740 \uc815\ub82c \ud6a8\uacfc\ub97c \ubc29\uc9c0\ud558\uace0\uc790 \ub370\uc774\ud130\ub97c \ubb34\uc791\uc704\ub85c \ucd94\ucd9c\ud558\uaca0\uc2b5\ub2c8\ub2e4. \ub610\ud55c \uc77c\ubc18\uc801\uc73c\ub85c \uc0ac\uc6a9\ud558\ub294 \ud559\uc2b5\ub960 \ubc94\uc704\uc5d0\uc11c \ubcf4\ub2e4 \uc27d\uac8c \ud559\uc2b5\ud560 \uc218 \uc788\ub3c4\ub85d `median_house_value`\ub97c \ucc9c \ub2e8\uc704\ub85c \uc870\uc815\ud558\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "r0eVyguIU80m",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "california_housing_dataframe = california_housing_dataframe.reindex(\n",
        "    np.random.permutation(california_housing_dataframe.index))\n",
        "california_housing_dataframe[\"median_house_value\"] /= 1000.0\n",
        "california_housing_dataframe"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "HzzlSs3PtTmt",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \ub370\uc774\ud130 \uc870\uc0ac\n",
        "\n",
        "\ub370\uc774\ud130\ub97c \ubcf8\uaca9\uc801\uc73c\ub85c \ub2e4\ub8e8\uae30 \uc804\uc5d0 \uc7a0\uc2dc \uc0b4\ud3b4\ubcf4\ub294 \uac83\uc774 \uc88b\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uac01 \uc5f4\uc5d0 \ub300\ud574 \uc608\uc758 \uac1c\uc218, \ud3c9\uade0, \ud45c\uc900\ud3b8\ucc28, \ucd5c\ub300\uac12, \ucd5c\uc18c\uac12, \ub2e4\uc591\ud55c \ubd84\uc704 \ub4f1 \uba87 \uac00\uc9c0 \uc720\uc6a9\ud55c \ud1b5\uacc4\ub97c \uac04\ub2e8\ud788 \uc694\uc57d\ud558\uc5ec \ucd9c\ub825\ud574 \ubcf4\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "gzb10yoVrydW",
        "colab_type": "code",
        "slideshow": {
          "slide_type": "slide"
        },
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "test": {
            "output": "ignore",
            "timeout": 600
          }
        },
        "cellView": "both"
      },
      "source": [
        "california_housing_dataframe.describe()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Lr6wYl2bt2Ep",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \uccab \ubc88\uc9f8 \ubaa8\ub378 \ub9cc\ub4e4\uae30\n",
        "\n",
        "\uc774 \uc2e4\uc2b5\uc5d0\uc11c\ub294 \ub77c\ubca8(\ud0c0\uac9f\uc774\ub77c\uace0\ub3c4 \ud568) \uc5ed\ud560\uc744 \ud558\ub294 `median_house_value`\uc5d0 \ub300\ud55c \uc608\uce21\uc744 \uc2dc\ub3c4\ud569\ub2c8\ub2e4. \uc785\ub825 \ud2b9\uc131\uc73c\ub85c\ub294 `total_rooms`\ub97c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.\n",
        "\n",
        "**\ucc38\uace0:** \ub370\uc774\ud130\ub294 \uc9c0\uc5ed \ub2e8\uc704\uc774\ubbc0\ub85c \uc774 \ud2b9\uc131\uc740 \ud574\ub2f9 \uc9c0\uc5ed\uc758 \uc804\uccb4 \ubc29 \uc218\ub97c \ub098\ud0c0\ub0c5\ub2c8\ub2e4.\n",
        "\n",
        "\ubaa8\ub378\uc744 \ud559\uc2b5\uc2dc\ud0a4\ub824\uba74 \ud150\uc11c\ud50c\ub85c\uc6b0 [Estimator](https://www.tensorflow.org/get_started/estimator) API\uac00 \uc81c\uacf5\ud558\ub294 [LinearRegressor](https://www.tensorflow.org/api_docs/python/tf/estimator/LinearRegressor) \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \uc774 API\ub294 \uc800\uc218\uc900 \ubaa8\ub378 \uc791\uc5c5\uc744 \uc54c\uc544\uc11c \ucc98\ub9ac\ud558\uace0 \ubaa8\ub378 \ud559\uc2b5, \ud3c9\uac00, \ucd94\ub860\uc744 \uc218\ud589\ud558\ub294 \ub370 \ud3b8\ub9ac\ud558\uac8c \uc0ac\uc6a9\ub418\ub294 \uba54\uc18c\ub4dc\ub97c \ub178\ucd9c\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "0cpcsieFhsNI",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 1\ub2e8\uacc4: \ud2b9\uc131 \uc815\uc758 \ubc0f \ud2b9\uc131 \uc5f4 \uad6c\uc131"
      ]
    },
    {
      "metadata": {
        "id": "EL8-9d4ZJNR7",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ud559\uc2b5 \ub370\uc774\ud130\ub97c \ud150\uc11c\ud50c\ub85c\uc6b0\ub85c \uac00\uc838\uc624\ub824\uba74 \uac01 \ud2b9\uc131\uc5d0 \ub4e4\uc5b4\uc788\ub294 \ub370\uc774\ud130 \uc720\ud615\uc744 \uc9c0\uc815\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774 \uc2e4\uc2b5\uacfc \uc774\ud6c4 \uc2e4\uc2b5\uc5d0\uc11c\ub294 \uc8fc\ub85c 2\uac00\uc9c0 \ub370\uc774\ud130 \uc720\ud615\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.\n",
        "\n",
        "* **\ubc94\uc8fc\ud615 \ub370\uc774\ud130**: \ud14d\uc2a4\ud2b8\ub85c \uc774\ub8e8\uc5b4\uc9c4 \ub370\uc774\ud130\uc785\ub2c8\ub2e4. \uc774 \uc2e4\uc2b5\uc758 \uc8fc\ud0dd \ub370\uc774\ud130 \uc138\ud2b8\ub294 \ubc94\uc8fc\ud615 \ub370\uc774\ud130\ub97c \ud3ec\ud568\ud558\uc9c0 \uc54a\uc9c0\ub9cc \uc8fc\ud0dd \uc591\uc2dd, \ubd80\ub3d9\uc0b0 \uad11\uace0 \ubb38\uad6c \ub4f1\uc758 \uc608\ub97c \ubcf4\uac8c \ub420 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "* **\uc218\uce58 \ub370\uc774\ud130**: \uc815\uc218 \ub610\ub294 \ubd80\ub3d9 \uc18c\uc218\uc810 \uc22b\uc790\uc774\uba70 \uc22b\uc790\ub85c \ucde8\uae09\ud558\ub824\ub294 \ub370\uc774\ud130\uc785\ub2c8\ub2e4. \uc774\ud6c4\uc5d0\ub3c4 \uc124\uba85\ud558\uaca0\uc9c0\ub9cc, \uc6b0\ud3b8\ubc88\ud638 \ub4f1\uc758 \uc218\uce58 \ub370\uc774\ud130\ub294 \ubc94\uc8fc\ud615\uc73c\ub85c \ucde8\uae09\ud558\ub294 \uacbd\uc6b0\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\ud150\uc11c\ud50c\ub85c\uc6b0\uc5d0\uc11c \ud2b9\uc131\uc758 \ub370\uc774\ud130 \uc720\ud615\uc744 \uc9c0\uc815\ud558\ub824\uba74 **\ud2b9\uc131 \uc5f4**\uc774\ub77c\ub294 \uad6c\uc870\uccb4\ub97c \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \ud2b9\uc131 \uc5f4\uc740 \ud2b9\uc131 \ub370\uc774\ud130\uc5d0 \ub300\ud55c \uc124\uba85\ub9cc \uc800\uc7a5\ud558\uba70 \ud2b9\uc131 \ub370\uc774\ud130 \uc790\uccb4\ub294 \ud3ec\ud568\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uc6b0\uc120\uc740 `total_rooms`\ub77c\ub294 \uc218\uce58 \uc785\ub825 \ub370\uc774\ud130 \ud558\ub098\ub9cc \uc0ac\uc6a9\ud558\uaca0\uc2b5\ub2c8\ub2e4. \ub2e4\uc74c \ucf54\ub4dc\uc5d0\uc11c\ub294 `california_housing_dataframe`\uc5d0\uc11c `total_rooms` \ub370\uc774\ud130\ub97c \ucd94\ucd9c\ud558\uace0 `numeric_column`\uc73c\ub85c \ud2b9\uc131 \uc5f4\uc744 \uc815\uc758\ud558\uc5ec \ub370\uc774\ud130\uac00 \uc22b\uc790\uc784\uc744 \uc9c0\uc815\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "rhEbFCZ86cDZ",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Define the input feature: total_rooms.\n",
        "my_feature = california_housing_dataframe[[\"total_rooms\"]]\n",
        "\n",
        "# Configure a numeric feature column for total_rooms.\n",
        "feature_columns = [tf.feature_column.numeric_column(\"total_rooms\")]"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "K_3S8teX7Rd2",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\ucc38\uace0:** `total_rooms` \ub370\uc774\ud130\ub294 1\ucc28\uc6d0 \ubc30\uc5f4(\uac01 \uc9c0\uc5ed\uc758 \uc804\uccb4 \ubc29 \uc218\ub85c \uc774\ub8e8\uc5b4\uc9c4 \ubaa9\ub85d) \ud615\ud0dc\uc785\ub2c8\ub2e4. \uc774\ub294 `numeric_column`\uc758 \uae30\ubcf8 \ud615\ud0dc\uc774\ubbc0\ub85c \uc778\uc218\ub85c \uc804\ub2ec\ud560 \ud544\uc694\uac00 \uc5c6\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "UMl3qrU5MGV6",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 2\ub2e8\uacc4: \ud0c0\uac9f \uc815\uc758"
      ]
    },
    {
      "metadata": {
        "id": "cw4nrfcB7kyk",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub2e4\uc74c\uc73c\ub85c\ub294 \ud0c0\uac9f\uc778 `median_house_value`\ub97c \uc815\uc758\ud569\ub2c8\ub2e4. \uc774 \ub370\uc774\ud130\ub3c4 `california_housing_dataframe`\uc5d0\uc11c \uac00\uc838\uc635\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "l1NvvNkH8Kbt",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Define the label.\n",
        "targets = california_housing_dataframe[\"median_house_value\"]"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "4M-rTFHL2UkA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 3\ub2e8\uacc4: LinearRegressor \uad6c\uc131"
      ]
    },
    {
      "metadata": {
        "id": "fUfGQUNp7jdL",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub2e4\uc74c\uc73c\ub85c\ub294 LinearRegressor\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc120\ud615 \ud68c\uadc0 \ubaa8\ub378\uc744\uad6c\uc131\ud569\ub2c8\ub2e4. \ubbf8\ub2c8 \ubc30\uce58 \ud655\ub960\uc801 \uacbd\uc0ac\ud558\uac15\ubc95(SGD)\uc744 \uad6c\ud604\ud558\ub294 `GradientDescentOptimizer`\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc774 \ubaa8\ub378\uc744 \ud559\uc2b5\uc2dc\ud0ac \uac83\uc785\ub2c8\ub2e4. `learning_rate` \uc778\uc218\ub294 \uacbd\uc0ac \ub2e8\uacc4\uc758 \ud06c\uae30\ub97c \uc870\uc808\ud569\ub2c8\ub2e4.\n",
        "\n",
        "**\ucc38\uace0:** \uc548\uc804\uc744 \uc704\ud574 \uc635\ud2f0\ub9c8\uc774\uc800\uc5d0 `clip_gradients_by_norm`\uc744 \ud1b5\ud574 [\uacbd\uc0ac \uc81c\ud55c](https://developers.google.com/machine-learning/glossary/#gradient_clipping)\uc744 \uc801\uc6a9\ud569\ub2c8\ub2e4. \uacbd\uc0ac \uc81c\ud55c\uc740 \ud559\uc2b5 \uc911\uc5d0 \uacbd\uc0ac\uac00 \ub108\ubb34 \ucee4\uc838\uc11c \uacbd\uc0ac\ud558\uac15\ubc95\uc774 \uc2e4\ud328\ud558\ub294 \uacbd\uc6b0\uac00 \ub098\ud0c0\ub098\uc9c0 \uc54a\ub3c4\ub85d \uc81c\ud55c\ud569\ub2c8\ub2e4. "
      ]
    },
    {
      "metadata": {
        "id": "ubhtW-NGU802",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Use gradient descent as the optimizer for training the model.\n",
        "my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)\n",
        "my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)\n",
        "\n",
        "# Configure the linear regression model with our feature columns and optimizer.\n",
        "# Set a learning rate of 0.0000001 for Gradient Descent.\n",
        "linear_regressor = tf.estimator.LinearRegressor(\n",
        "    feature_columns=feature_columns,\n",
        "    optimizer=my_optimizer\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-0IztwdK2f3F",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 4\ub2e8\uacc4: \uc785\ub825 \ud568\uc218 \uc815\uc758"
      ]
    },
    {
      "metadata": {
        "id": "S5M5j6xSCHxx",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uce98\ub9ac\ud3ec\ub2c8\uc544 \uc8fc\ud0dd \ub370\uc774\ud130\ub97c `LinearRegressor`\ub85c \uac00\uc838\uc624\ub824\uba74 \ud150\uc11c\ud50c\ub85c\uc6b0\uc5d0 \ub370\uc774\ud130 \uc804\ucc98\ub9ac \ubc29\ubc95 \ubc0f \ubaa8\ub378 \ud559\uc2b5 \uc911\uc758 \uc77c\uad04 \ucc98\ub9ac, \uc154\ud50c, \ubc18\ubcf5 \ubc29\ubc95\uc744 \uc54c\ub824\uc8fc\ub294 \uc785\ub825 \ud568\uc218\ub97c \uc815\uc758\ud574\uc57c \ud569\ub2c8\ub2e4.\n",
        "\n",
        "\uc6b0\uc120 *pandas* \ud2b9\uc131 \ub370\uc774\ud130\ub97c NumPy \ubc30\uc5f4\uc758 dict\ub85c \ubcc0\ud658\ud569\ub2c8\ub2e4. \uadf8\ub7f0 \ub2e4\uc74c \ud150\uc11c\ud50c\ub85c\uc6b0\uc758 [Dataset API](https://www.tensorflow.org/programmers_guide/datasets)\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc774 \ub370\uc774\ud130\ub85c\ubd80\ud130 \ub370\uc774\ud130 \uc138\ud2b8 \uac1c\uccb4\ub97c \uc0dd\uc131\ud558\uace0 `batch_size` \ud06c\uae30\uc758 \ubc30\uce58\ub85c \ub098\ub204\uc5b4 \uc9c0\uc815\ud55c \uc138\ub300 \uc218(num_epochs)\ub9cc\ud07c \ubc18\ubcf5\ud569\ub2c8\ub2e4.\n",
        "\n",
        "**\ucc38\uace0:** \uae30\ubcf8\uac12\uc778 `num_epochs=None`\uc744 `repeat()`\uc5d0 \uc804\ub2ec\ud558\uba74 \uc785\ub825 \ub370\uc774\ud130\uac00 \ubb34\ud55c\uc815 \ubc18\ubcf5\ub429\ub2c8\ub2e4.\n",
        "\n",
        "\ub2e4\uc74c\uc73c\ub85c, `shuffle`\uc744 `True`\ub85c \uc124\uc815\ud558\uba74 \ud559\uc2b5 \uc911\uc5d0 \ub370\uc774\ud130\uac00 \ubaa8\ub378\uc5d0 \ubb34\uc791\uc704\ub85c \uc804\ub2ec\ub418\ub3c4\ub85d \ub370\uc774\ud130\uac00 \ub4a4\uc11e\uc785\ub2c8\ub2e4. `buffer_size` \uc778\uc218\ub294 `shuffle`\uc5d0\uc11c \ubb34\uc791\uc704\ub85c \ucd94\ucd9c\ud560 \ub370\uc774\ud130 \uc138\ud2b8\uc758 \ud06c\uae30\ub97c \uc9c0\uc815\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\ub9c8\uc9c0\ub9c9\uc73c\ub85c \uc785\ub825 \ud568\uc218\uc5d0\uc11c \ub370\uc774\ud130 \uc138\ud2b8\uc5d0 \ub300\ud55c \ubc18\ubcf5\uc790\ub97c \ub9cc\ub4e4\uace0 \ub2e4\uc74c \ub370\uc774\ud130 \ubc30\uce58\ub97c LinearRegressor\uc5d0 \ubc18\ud658\ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "RKZ9zNcHJtwc",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):\n",
        "    \"\"\"Trains a linear regression model of one feature.\n",
        "  \n",
        "    Args:\n",
        "      features: pandas DataFrame of features\n",
        "      targets: pandas DataFrame of targets\n",
        "      batch_size: Size of batches to be passed to the model\n",
        "      shuffle: True or False. Whether to shuffle the data.\n",
        "      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely\n",
        "    Returns:\n",
        "      Tuple of (features, labels) for next data batch\n",
        "    \"\"\"\n",
        "  \n",
        "    # Convert pandas data into a dict of np arrays.\n",
        "    features = {key:np.array(value) for key,value in dict(features).items()}                                           \n",
        " \n",
        "    # Construct a dataset, and configure batching/repeating.\n",
        "    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit\n",
        "    ds = ds.batch(batch_size).repeat(num_epochs)\n",
        "    \n",
        "    # Shuffle the data, if specified.\n",
        "    if shuffle:\n",
        "      ds = ds.shuffle(buffer_size=10000)\n",
        "    \n",
        "    # Return the next batch of data.\n",
        "    features, labels = ds.make_one_shot_iterator().get_next()\n",
        "    return features, labels"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "wwa6UeA1V5F_",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\ucc38\uace0:** \uc774\ud6c4 \uc2e4\uc2b5\uc5d0\ub3c4 \uc774 \uc785\ub825 \ud568\uc218\ub97c \uacc4\uc18d \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \uc785\ub825 \ud568\uc218 \ubc0f `Dataset` API\uc5d0 \ub300\ud55c \uc790\uc138\ud55c \ub0b4\uc6a9\uc740 [\ud150\uc11c\ud50c\ub85c\uc6b0 \ud504\ub85c\uadf8\ub798\uba38 \uac00\uc774\ub4dc](https://www.tensorflow.org/programmers_guide/datasets)\ub97c \ucc38\uc870\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "4YS50CQb2ooO",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 5\ub2e8\uacc4: \ubaa8\ub378 \ud559\uc2b5"
      ]
    },
    {
      "metadata": {
        "id": "yP92XkzhU803",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc774\uc81c linear_regressor\ub85c\ubd80\ud130 train()\uc744 \ud638\ucd9c\ud558\uc5ec \ubaa8\ub378\uc744 \ud559\uc2b5\uc2dc\ud0ac \uc218 \uc788\uc2b5\ub2c8\ub2e4. `my_feature` \ubc0f `target`\uc744 \uc778\uc218\ub85c \uc804\ub2ec\ud560 \uc218 \uc788\ub3c4\ub85d `my_input_fn`\uc744 `lambda`\uc5d0 \ub798\ud551\ud558\uaca0\uc2b5\ub2c8\ub2e4. \uc790\uc138\ud55c \ub0b4\uc6a9\uc740 [\ud150\uc11c\ud50c\ub85c\uc6b0 \uc785\ub825 \ud568\uc218 \uac00\uc774\ub4dc](https://www.tensorflow.org/get_started/input_fn#passing_input_fn_data_to_your_model)\ub97c \ucc38\uc870\ud558\uc138\uc694. \ucc98\uc74c\uc5d0\ub294 100\ub2e8\uacc4\ub9cc \ud559\uc2b5\ud558\ub824\uace0 \ud569\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "5M-Kt6w8U803",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_ = linear_regressor.train(\n",
        "    input_fn = lambda:my_input_fn(my_feature, targets),\n",
        "    steps=100\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "7Nwxqxlx2sOv",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### 6\ub2e8\uacc4: \ubaa8\ub378 \ud3c9\uac00"
      ]
    },
    {
      "metadata": {
        "id": "KoDaF2dlJQG5",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ubaa8\ub378\uc774 \ud559\uc2b5 \uc911\uc5d0 \ud559\uc2b5 \ub370\uc774\ud130\uc5d0 \uc5bc\ub9c8\ub098 \ub9de\ucdb0\uc84c\ub294\uc9c0 \ud655\uc778\ud558\uae30 \uc704\ud574 \ud559\uc2b5 \ub370\uc774\ud130\ub85c \uc608\uce21\uc744 \uc2e4\ud589\ud558\uaca0\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "**\ucc38\uace0:** \ud559\uc2b5 \uc624\ucc28\ub294 \ubaa8\ub378\uc774 \ud559\uc2b5 \ub370\uc774\ud130\uc5d0 \uc5bc\ub9c8\ub098 \ub9de\ucdb0\uc84c\ub294\uc9c0\ub97c \ub098\ud0c0\ub0b4\ub294 \ucc99\ub3c4\uc774\uc9c0\ub9cc \ubaa8\ub378\uc774 **_\uc0c8 \ub370\uc774\ud130\ub85c \uc77c\ubc18\ud654_**\ub418\ub294 \uc815\ub3c4\ub97c \uce21\uc815\ud558\uc9c0\ub294 **_\uc54a\uc2b5\ub2c8\ub2e4._** \uc774\ud6c4 \uc2e4\uc2b5\uc5d0\uc11c\ub294 \ubaa8\ub378\uc758 \uc77c\ubc18\ud654 \ub2a5\ub825\uc744 \ud3c9\uac00\ud560 \uc218 \uc788\ub3c4\ub85d \ub370\uc774\ud130\ub97c \ubd84\ud560\ud558\ub294 \ubc29\ubc95\uc744 \uc54c\uc544\ubd05\ub2c8\ub2e4.\n"
      ]
    },
    {
      "metadata": {
        "id": "pDIxp6vcU809",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Create an input function for predictions.\n",
        "# Note: Since we're making just one prediction for each example, we don't \n",
        "# need to repeat or shuffle the data here.\n",
        "prediction_input_fn =lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)\n",
        "\n",
        "# Call predict() on the linear_regressor to make predictions.\n",
        "predictions = linear_regressor.predict(input_fn=prediction_input_fn)\n",
        "\n",
        "# Format predictions as a NumPy array, so we can calculate error metrics.\n",
        "predictions = np.array([item['predictions'][0] for item in predictions])\n",
        "\n",
        "# Print Mean Squared Error and Root Mean Squared Error.\n",
        "mean_squared_error = metrics.mean_squared_error(predictions, targets)\n",
        "root_mean_squared_error = math.sqrt(mean_squared_error)\n",
        "print(\"Mean Squared Error (on training data): %0.3f\" % mean_squared_error)\n",
        "print(\"Root Mean Squared Error (on training data): %0.3f\" % root_mean_squared_error)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "AKWstXXPzOVz",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " \uc6b0\uc218\ud55c \ubaa8\ub378\uc778\uac00\uc694? \uc774 \uc624\ucc28\uac00 \uc5bc\ub9c8\ub098 \ud070\uc9c0 \uc5b4\ub5bb\uac8c \ud310\ub2e8\ud560 \uc218 \uc788\uc744\uae4c\uc694?\n",
        "\n",
        "\ud3c9\uade0 \uc81c\uacf1 \uc624\ucc28(MSE)\ub294 \ud574\uc11d\ud558\uae30\uac00 \uc5b4\ub824\uc6b8 \uc218 \uc788\uc73c\ubbc0\ub85c \ud3c9\uade0 \uc81c\uacf1\uadfc \uc624\ucc28(RMSE)\ub97c \ub300\uc2e0 \ucc38\uace0\ud558\ub294 \uacbd\uc6b0\uac00 \ub9ce\uc2b5\ub2c8\ub2e4. RMSE\uc758 \uc7a5\uc810\uc740 \uc6d0\ub798 \ud0c0\uac9f\uacfc \ub3d9\uc77c\ud55c \ucc99\ub3c4\ub85c \ud574\uc11d\ud560 \uc218 \uc788\ub2e4\ub294 \uac83\uc785\ub2c8\ub2e4.\n",
        "\n",
        "RMSE\ub97c \ud0c0\uac9f\uc758 \ucd5c\uc18c\uac12\uacfc \ucd5c\ub300\uac12\uc758 \ucc28\uc640 \ube44\uad50\ud574 \ubcf4\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "7UwqGbbxP53O",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "min_house_value = california_housing_dataframe[\"median_house_value\"].min()\n",
        "max_house_value = california_housing_dataframe[\"median_house_value\"].max()\n",
        "min_max_difference = max_house_value - min_house_value\n",
        "\n",
        "print(\"Min. Median House Value: %0.3f\" % min_house_value)\n",
        "print(\"Max. Median House Value: %0.3f\" % max_house_value)\n",
        "print(\"Difference between Min. and Max.: %0.3f\" % min_max_difference)\n",
        "print(\"Root Mean Squared Error: %0.3f\" % root_mean_squared_error)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "JigJr0C7Pzit",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc624\ucc28 \ubc94\uc704\uac00 \ud0c0\uac9f \uac12 \ubc94\uc704\uc758 \uac70\uc758 \uc808\ubc18\uc5d0 \ub2ec\ud569\ub2c8\ub2e4. \uc624\ucc28\ub97c \uc774\ubcf4\ub2e4 \uc904\uc77c \uc218 \uc788\uc744\uae4c\uc694?\n",
        "\n",
        "\uc774 \uc9c8\ubb38\uc774\uc57c\ub9d0\ub85c \ubaa8\ub4e0 \ubaa8\ub378 \uac1c\ubc1c\uc790\ub4e4\uc758 \uc219\uc81c\uc785\ub2c8\ub2e4. \ubaa8\ub378 \uc624\ucc28\ub97c \uc904\uc774\ub294 \uba87 \uac00\uc9c0 \uae30\ubcf8\uc801\uc778 \uc804\ub7b5\uc744 \uc218\ub9bd\ud574 \ubcf4\uaca0\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uac00\uc7a5 \ucc98\uc74c\uc5d0 \ud560 \uc218 \uc788\ub294 \uc77c\uc740 \uc804\ubc18\uc801 \uc694\uc57d \ud1b5\uacc4\ub97c \ucc38\uc870\ud558\uc5ec \uc608\uce21\uacfc \ud0c0\uac9f\uc758 \uc77c\uce58\uc728\uc744 \uc870\uc0ac\ud558\ub294 \uac83\uc785\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "941nclxbzqGH",
        "colab_type": "code",
        "slideshow": {
          "slide_type": "-"
        },
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "test": {
            "output": "ignore",
            "timeout": 600
          }
        },
        "cellView": "both"
      },
      "source": [
        "calibration_data = pd.DataFrame()\n",
        "calibration_data[\"predictions\"] = pd.Series(predictions)\n",
        "calibration_data[\"targets\"] = pd.Series(targets)\n",
        "calibration_data.describe()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "E2-bf8Hq36y8",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "cell_type": "markdown",
      "source": [
        " \uc774 \uc815\ubcf4\ub294 \uc720\uc6a9\ud574 \ubcf4\uc785\ub2c8\ub2e4. \ud3c9\uade0 \uac12\uc744 \ubaa8\ub378\uc758 RMSE\uc640 \ube44\uad50\ud574 \ubcf4\uba74 \uc5b4\ub5a0\ud55c\uac00\uc694? \ub2e4\uc591\ud55c \ubd84\uc704\ub294 \uc5b4\ub5a0\ud55c\uac00\uc694?\n",
        "\n",
        "\ud559\uc2b5\ud55c \ub370\uc774\ud130\uc640 \uc120\uc744 \uc2dc\uac01\ud654\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \ub2e8\uc77c \ud2b9\uc131\uc5d0 \ub300\ud55c \uc120\ud615 \ud68c\uadc0\ub294 \uc785\ub825 *x*\ub97c \ucd9c\ub825 *y*\uc5d0 \ub9e4\ud551\ud558\ub294 \uc9c1\uc120\uc73c\ub85c \ud45c\ud604\ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uc6b0\uc120 \ud310\ub3c5 \uac00\ub2a5\ud55c \uc0b0\ud3ec\ub3c4\ub97c \uadf8\ub9b4 \uc218 \uc788\ub3c4\ub85d \uade0\uc77c\ud55c \ubb34\uc791\uc704 \ub370\uc774\ud130 \uc0d8\ud50c\uc744 \ucd94\ucd9c\ud558\uaca0\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "SGRIi3mAU81H",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "sample = california_housing_dataframe.sample(n=300)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "N-JwuJBKU81J",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \ub2e4\uc74c\uc73c\ub85c\ub294 \uc0b0\ud3ec\ub3c4\uc640 \ud568\uaed8 \ubaa8\ub378\uc758 \ubc14\uc774\uc5b4\uc2a4 \ud56d\uc640 \ud2b9\uc131 \uac00\uc911\uce58\ub97c \ubc14\ud0d5\uc73c\ub85c \ud559\uc2b5\ud55c \uc120\uc744 \uadf8\ub9ac\uaca0\uc2b5\ub2c8\ub2e4. \uc774 \uc120\uc740 \ube68\uac04\uc0c9\uc73c\ub85c \ud45c\uc2dc\ub429\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "7G12E76-339G",
        "colab_type": "code",
        "slideshow": {
          "slide_type": "-"
        },
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "test": {
            "output": "ignore",
            "timeout": 600
          }
        },
        "cellView": "both"
      },
      "source": [
        "# Get the min and max total_rooms values.\n",
        "x_0 = sample[\"total_rooms\"].min()\n",
        "x_1 = sample[\"total_rooms\"].max()\n",
        "\n",
        "# Retrieve the final weight and bias generated during training.\n",
        "weight = linear_regressor.get_variable_value('linear/linear_model/total_rooms/weights')[0]\n",
        "bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')\n",
        "\n",
        "# Get the predicted median_house_values for the min and max total_rooms values.\n",
        "y_0 = weight * x_0 + bias \n",
        "y_1 = weight * x_1 + bias\n",
        "\n",
        "# Plot our regression line from (x_0, y_0) to (x_1, y_1).\n",
        "plt.plot([x_0, x_1], [y_0, y_1], c='r')\n",
        "\n",
        "# Label the graph axes.\n",
        "plt.ylabel(\"median_house_value\")\n",
        "plt.xlabel(\"total_rooms\")\n",
        "\n",
        "# Plot a scatter plot from our data sample.\n",
        "plt.scatter(sample[\"total_rooms\"], sample[\"median_house_value\"])\n",
        "\n",
        "# Display graph.\n",
        "plt.show()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "t0lRt4USU81L",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc774 \ucd5c\ucd08 \uc120\uc740 \uc0c1\ub2f9\ud788 \uc774\ud0c8\ub41c \uc0c1\ud0dc\uc785\ub2c8\ub2e4. \uc694\uc57d \ud1b5\uacc4\ub97c \ub2e4\uc2dc \uc870\uc0ac\ud558\uc5ec \uc774 \uc120\uc774 \ub098\ud0c0\ub0b4\ub294 \uc815\ubcf4\uc640 \ubd80\ud569\ud558\ub294\uc9c0 \ud655\uc778\ud574 \ubcf4\uc138\uc694.\n",
        "\n",
        "\uc774\ub7ec\ud55c \ucd08\uae30 \uc0c1\ud0dc \ud655\uc778\uc744 \ud1b5\ud574 \ud6e8\uc52c \ub354 \ub098\uc740 \uc120\uc744 \ucc3e\uc744 \uc218 \uc788\ub2e4\ub294 \uc810\uc744 \uc54c \uc218 \uc788\uc2b5\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "AZWF67uv0HTG",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \ubaa8\ub378 \ucd08\ub9e4\uac1c\ubcc0\uc218 \uc870\uc815\n",
        "\uc774 \uc2e4\uc2b5\uc5d0\uc11c\ub294 \ud3b8\uc758\ub97c \uc704\ud574 \uc704 \ubaa8\ub4e0 \ucf54\ub4dc\ub97c \ub2e8\uc77c \ud568\uc218\uc5d0 \ub123\uc5c8\uc2b5\ub2c8\ub2e4. \ub2e4\ub978 \ub9e4\uac1c\ubcc0\uc218\ub85c \ud568\uc218\ub97c \ud638\ucd9c\ud558\uc5ec \ud6a8\uacfc\ub97c \ud655\uc778\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uc774 \ud568\uc218\uc5d0\uc11c \uade0\ub4f1\ud558\uac8c 10\uac1c\ub85c \ub098\ub208 \uae30\uac04\uc73c\ub85c \ud559\uc2b5\uc744 \uc9c4\ud589\ud558\uc5ec \ubaa8\ub378\uc758 \uac1c\uc120\uc744 \uae30\uac04\ubcc4\ub85c \uc0b4\ud3b4\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\uac01 \uae30\uac04\uc5d0 \ub300\ud55c \ud559\uc2b5 \uc190\uc2e4\uc744 \uacc4\uc0b0\ud558\uace0 \uadf8\ub798\ud504\ub85c \uadf8\ub9ac\uaca0\uc2b5\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \ubaa8\ub378\uc774 \uc218\ub834\ub418\ub294 \uc2dc\uc810\uc744 \ud310\ub2e8\ud558\uac70\ub098 \ubc18\ubcf5\uc774 \ub354 \ud544\uc694\ud568\uc744 \ud655\uc778\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        "\ub610\ud55c \ubaa8\ub378\uc774 \ud559\uc2b5\ud55c \ud2b9\uc131 \uac00\uc911\uce58\uc640 \ubc14\uc774\uc5b4\uc2a4 \ud56d\uc744 \uc2dc\uac04\ubcc4\ub85c \ub3c4\uc2dd\ud654\ud558\uaca0\uc2b5\ub2c8\ub2e4. \uc774\ub294 \ubaa8\ub378\uc774 \uc218\ub834\ub418\ub294 \ubaa8\uc2b5\uc744 \ud655\uc778\ud558\ub294 \ub610 \ub2e4\ub978 \ubc29\ubc95\uc785\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "wgSMeD5UU81N",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def train_model(learning_rate, steps, batch_size, input_feature=\"total_rooms\"):\n",
        "  \"\"\"Trains a linear regression model of one feature.\n",
        "  \n",
        "  Args:\n",
        "    learning_rate: A `float`, the learning rate.\n",
        "    steps: A non-zero `int`, the total number of training steps. A training step\n",
        "      consists of a forward and backward pass using a single batch.\n",
        "    batch_size: A non-zero `int`, the batch size.\n",
        "    input_feature: A `string` specifying a column from `california_housing_dataframe`\n",
        "      to use as input feature.\n",
        "  \"\"\"\n",
        "  \n",
        "  periods = 10\n",
        "  steps_per_period = steps / periods\n",
        "\n",
        "  my_feature = input_feature\n",
        "  my_feature_data = california_housing_dataframe[[my_feature]]\n",
        "  my_label = \"median_house_value\"\n",
        "  targets = california_housing_dataframe[my_label]\n",
        "\n",
        "  # Create feature columns.\n",
        "  feature_columns = [tf.feature_column.numeric_column(my_feature)]\n",
        "  \n",
        "  # Create input functions.\n",
        "  training_input_fn = lambda:my_input_fn(my_feature_data, targets, batch_size=batch_size)\n",
        "  prediction_input_fn = lambda: my_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)\n",
        "  \n",
        "  # Create a linear regressor object.\n",
        "  my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
        "  my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)\n",
        "  linear_regressor = tf.estimator.LinearRegressor(\n",
        "      feature_columns=feature_columns,\n",
        "      optimizer=my_optimizer\n",
        "  )\n",
        "\n",
        "  # Set up to plot the state of our model's line each period.\n",
        "  plt.figure(figsize=(15, 6))\n",
        "  plt.subplot(1, 2, 1)\n",
        "  plt.title(\"Learned Line by Period\")\n",
        "  plt.ylabel(my_label)\n",
        "  plt.xlabel(my_feature)\n",
        "  sample = california_housing_dataframe.sample(n=300)\n",
        "  plt.scatter(sample[my_feature], sample[my_label])\n",
        "  colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]\n",
        "\n",
        "  # Train the model, but do so inside a loop so that we can periodically assess\n",
        "  # loss metrics.\n",
        "  print(\"Training model...\")\n",
        "  print(\"RMSE (on training data):\")\n",
        "  root_mean_squared_errors = []\n",
        "  for period in range (0, periods):\n",
        "    # Train the model, starting from the prior state.\n",
        "    linear_regressor.train(\n",
        "        input_fn=training_input_fn,\n",
        "        steps=steps_per_period\n",
        "    )\n",
        "    # Take a break and compute predictions.\n",
        "    predictions = linear_regressor.predict(input_fn=prediction_input_fn)\n",
        "    predictions = np.array([item['predictions'][0] for item in predictions])\n",
        "    \n",
        "    # Compute loss.\n",
        "    root_mean_squared_error = math.sqrt(\n",
        "        metrics.mean_squared_error(predictions, targets))\n",
        "    # Occasionally print the current loss.\n",
        "    print(\"  period %02d : %0.2f\" % (period, root_mean_squared_error))\n",
        "    # Add the loss metrics from this period to our list.\n",
        "    root_mean_squared_errors.append(root_mean_squared_error)\n",
        "    # Finally, track the weights and biases over time.\n",
        "    # Apply some math to ensure that the data and line are plotted neatly.\n",
        "    y_extents = np.array([0, sample[my_label].max()])\n",
        "    \n",
        "    weight = linear_regressor.get_variable_value('linear/linear_model/%s/weights' % input_feature)[0]\n",
        "    bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')\n",
        "\n",
        "    x_extents = (y_extents - bias) / weight\n",
        "    x_extents = np.maximum(np.minimum(x_extents,\n",
        "                                      sample[my_feature].max()),\n",
        "                           sample[my_feature].min())\n",
        "    y_extents = weight * x_extents + bias\n",
        "    plt.plot(x_extents, y_extents, color=colors[period]) \n",
        "  print(\"Model training finished.\")\n",
        "\n",
        "  # Output a graph of loss metrics over periods.\n",
        "  plt.subplot(1, 2, 2)\n",
        "  plt.ylabel('RMSE')\n",
        "  plt.xlabel('Periods')\n",
        "  plt.title(\"Root Mean Squared Error vs. Periods\")\n",
        "  plt.tight_layout()\n",
        "  plt.plot(root_mean_squared_errors)\n",
        "\n",
        "  # Output a table with calibration data.\n",
        "  calibration_data = pd.DataFrame()\n",
        "  calibration_data[\"predictions\"] = pd.Series(predictions)\n",
        "  calibration_data[\"targets\"] = pd.Series(targets)\n",
        "  display.display(calibration_data.describe())\n",
        "\n",
        "  print(\"Final RMSE (on training data): %0.2f\" % root_mean_squared_error)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "kg8A4ArBU81Q",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc791\uc5c5 1: 180 \uc774\ud558\uc758 RMSE \ub2ec\uc131\n",
        "\n",
        "\ubaa8\ub378 \ucd08\ub9e4\uac1c\ubcc0\uc218\ub97c \uc870\uc815\ud558\uc5ec \ud0c0\uac9f \ubd84\ud3ec\uc640 \ub354 \uc798 \uc77c\uce58\ud558\ub3c4\ub85d \uc190\uc2e4\uc744 \uac1c\uc120\ud569\ub2c8\ub2e4.\n",
        "5\ubd84\uc774 \uc9c0\ub098\ub3c4\ub85d RMSE\ub97c 180 \uc774\ud558\ub85c \ub5a8\uc5b4\ub728\ub9ac\uc9c0 \ubabb\ud55c \uacbd\uc6b0 \ud574\uacb0 \ubc29\ubc95\uc5d0\uc744 \ud655\uc778\ud558\uc5ec \uac00\ub2a5\ud55c \uc870\ud569\uc744 \uc54c\uc544\ubcf4\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "UzoZUSdLIolF",
        "colab_type": "code",
        "slideshow": {
          "slide_type": "slide"
        },
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "test": {
            "output": "ignore",
            "timeout": 600
          }
        },
        "cellView": "both"
      },
      "source": [
        "train_model(\n",
        "    learning_rate=0.00001,\n",
        "    steps=100,\n",
        "    batch_size=1\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ajVM7rkoYXeL",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\uac00\ub2a5\ud55c \ud574\uacb0 \ubc29\ubc95 \uc911 \ud558\ub098\ub97c \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "T3zmldDwYy5c",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "train_model(\n",
        "    learning_rate=0.00002,\n",
        "    steps=500,\n",
        "    batch_size=5\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "M8H0_D4vYa49",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \uc774\ub294 \uac00\ub2a5\ud55c \uc870\ud569 \uc911 \ud558\ub098\uc77c \ubfd0\uc774\uba70, \uc6b0\uc218\ud55c \uacb0\uacfc\ub97c \ub0b4\ub294 \ub2e4\ub978 \uc124\uc815 \uc870\ud569\uc774 \uc788\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc77c\ubc18\uc801\uc73c\ub85c \uc774 \uc2e4\uc2b5\uc758 \ubaa9\uc801\uc740 *\ucd5c\uc0c1\uc758* \uc124\uc815\uc744 \ucc3e\ub294 \uac83\uc774 \uc544\ub2c8\ub77c \ubaa8\ub378 \uad6c\uc131\uc744 \uc870\uc815\ud558\uba74 \uc608\uce21 \ud488\uc9c8\uc5d0 \uc5b4\ub5a0\ud55c \uc601\ud5a5\uc744 \uc8fc\ub294\uc9c0 \uac10\uc744 \uc7a1\ub294 \uac83\uc785\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "QU5sLyYTqzqL",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ### \ubaa8\ub378 \uc870\uc815\uc5d0 \ub300\ud55c \ud45c\uc900 \ud734\ub9ac\uc2a4\ud2f1\uc774 \uc788\ub294\uac00?\n",
        "\n",
        "\ud754\ud788 \uc81c\uae30\ub418\ub294 \uc9c8\ubb38\uc785\ub2c8\ub2e4. \ub2e8\uc801\uc73c\ub85c \ub9d0\ud574, \ub2e4\uc591\ud55c \ucd08\ub9e4\uac1c\ubcc0\uc218\uc758 \ud6a8\uacfc\ub294 \ub370\uc774\ud130\uc5d0 \ub530\ub77c \ub2e4\ub985\ub2c8\ub2e4. \ub530\ub77c\uc11c \uc54c\uae30 \uc27d\uace0 \ud655\uace0\ud55c \uaddc\uce59\uc740 \uc874\uc7ac\ud558\uc9c0 \uc54a\uc73c\uba70, \uc2e4\uc81c \ub370\uc774\ud130\ub85c \ud14c\uc2a4\ud2b8\ud558\ub294 \uacfc\uc815\uc774 \ud544\uc694\ud569\ub2c8\ub2e4.\n",
        "\n",
        "\uadf8\ub7ec\ub098 \uc720\uc6a9\ud558\uac8c \ucc38\uace0\ud560 \ub9cc\ud55c \uba87 \uac00\uc9c0 \uacbd\ud5d8\uce59\uc774 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "\n",
        " * \ud559\uc2b5 \uc624\ucc28\ub294 \uc810\ucc28 \uac10\uc18c\ud569\ub2c8\ub2e4. \ucc98\uc74c\uc5d0\ub294 \uae09\uaca9\ud788 \uac10\uc18c\ud558\ub2e4\uac00 \ud559\uc2b5\uc774 \uc218\ub834\ub428\uc5d0 \ub530\ub77c \uacb0\uad6d \ud55c\uacc4\uc5d0 \ub2e4\ub2e4\ub985\ub2c8\ub2e4.\n",
        " * \ud559\uc2b5\uc774 \uc218\ub834\ub418\uc9c0 \uc54a\uc558\ub2e4\uba74 \ub354 \uc624\ub798 \uc2e4\ud589\ud574 \ubcf4\uc138\uc694.\n",
        " * \ud559\uc2b5 \uc624\ucc28\uac00 \ub108\ubb34 \ucc9c\ucc9c\ud788 \uac10\uc18c\ud558\ub294 \uacbd\uc6b0 \ud559\uc2b5\ub960\uc744 \ub192\uc774\uba74 \ub354 \ube68\ub9ac \uac10\uc18c\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n",
        "   * \ud559\uc2b5\ub960\uc774 \ub108\ubb34 \ub192\ub2e4\uba74 \uc815\ubc18\ub300 \ud604\uc0c1\uc774 \ub098\ud0c0\ub098\uae30\ub3c4 \ud569\ub2c8\ub2e4.\n",
        " * \ud559\uc2b5 \uc624\ucc28\uac00 \ud06c\uac8c \uc694\ub3d9\ud55c\ub2e4\uba74 \ud559\uc2b5\ub960\uc744 \ub0ae\ucdb0\ubcf4\uc138\uc694.\n",
        "   * \ud559\uc2b5\ub960\uc744 \ub0ae\ucd94\uba74\uc11c \ub2e8\uacc4 \uc218 \ub610\ub294 \ubc30\uce58 \ud06c\uae30\ub97c \ub298\ub9ac\uba74 \uc88b\uc740 \uacb0\uacfc\uac00 \ub098\ud0c0\ub098\ub294 \uacbd\uc6b0\uac00 \ub9ce\uc2b5\ub2c8\ub2e4.\n",
        " * \ubc30\uce58 \ud06c\uae30\uac00 \ub108\ubb34 \uc791\uc544\ub3c4 \ubd88\uc548\uc815\uc131\uc774 \ub098\ud0c0\ub0a0 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ucc98\uc74c\uc5d0\ub294 100, 1000 \ub4f1\uc758 \ud070 \uac12\uc744 \uc0ac\uc6a9\ud55c \ud6c4 \uc131\ub2a5\uc774 \uc545\ud654\ub418\uc9c0 \uc54a\ub294 \uc120\uae4c\uc9c0 \ub0ae\ucd94\uc138\uc694.\n",
        "\n",
        "\ud6a8\uacfc\ub294 \ub370\uc774\ud130\uc5d0 \ub530\ub77c \ub2ec\ub77c\uc9c0\ubbc0\ub85c \uc774\ub7ec\ud55c \uacbd\ud5d8\uce59\uc744 \ubb34\uc870\uac74\uc801\uc73c\ub85c \ub530\ub77c\uc11c\ub294 \uc548 \ub429\ub2c8\ub2e4. \uc2e4\ud5d8\uacfc \uac80\uc99d\uc744 \ud56d\uc0c1 \ubc18\ubcf5\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "GpV-uF_cBCBU",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \uc791\uc5c5 2: \ub2e4\ub978 \ud2b9\uc131 \uc2e4\ud5d8\n",
        "\n",
        "`total_rooms` \ud2b9\uc131\uc744 `population` \ud2b9\uc131\uc73c\ub85c \ub300\uccb4\ud558\uba74 \uacb0\uacfc\uac00 \uac1c\uc120\ub418\ub294\uc9c0 \ud655\uc778\ud574 \ubd05\ub2c8\ub2e4.\n",
        "\n",
        "\uc774 \ubd80\ubd84\uc740 \ucd5c\ub300 5\ubd84\uae4c\uc9c0\ub9cc \uc9c4\ud589\ud558\uc2dc\uae30 \ubc14\ub78d\ub2c8\ub2e4."
      ]
    },
    {
      "metadata": {
        "id": "YMyOxzb0ZlAH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# YOUR CODE HERE"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ci1ISxxrZ7v0",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \ud574\uacb0 \ubc29\ubc95\n",
        "\n",
        "\uac00\ub2a5\ud55c \ud574\uacb0 \ubc29\ubc95 \uc911 \ud558\ub098\ub97c \ubcf4\ub824\uba74 \uc544\ub798\ub97c \ud074\ub9ad\ud558\uc138\uc694."
      ]
    },
    {
      "metadata": {
        "id": "SjdQQCduZ7BV",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "train_model(\n",
        "    learning_rate=0.00002,\n",
        "    steps=1000,\n",
        "    batch_size=5,\n",
        "    input_feature=\"population\"\n",
        ")"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    }
  ]
}
